<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\User;

class LoginController extends Controller
{
    /**
     * 退出
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function logout( Request $request )
    {
        $this->guard()->logout();

        $request->session()->invalidate();

        return redirect('/admin/login');
    }

    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    public function login( Request $request )
    {
        $name  = User::where('name', $request->username)->first();
        $email = User::where('email', $request->username)->first();
        $phone = User::where('phone', $request->username)->first();
        if( !empty($name) || !empty($email) || !empty($phone) ) {
            if( $name[ 'is_show' ] == 1 || $email[ 'is_show' ] == 1 || $phone[ 'is_show' ] == 1 ) {
                $this->validateLogin($request);

                // If the class is using the ThrottlesLogins trait, we can automatically throttle
                // the login attempts for this application. We'll key this by the username and
                // the IP address of the client making these requests into this application.
                if( $this->hasTooManyLoginAttempts($request) ) {
                    $this->fireLockoutEvent($request);
                    return $this->sendLockoutResponse($request);
                }

                if( $this->attemptLogin($request) ) {
                    return $this->sendLoginResponse($request);
                }

                // If the login attempt was unsuccessful we will increment the number of attempts
                // to login and redirect the user back to the login form. Of course, when this
                // user surpasses their maximum number of attempts they will get locked out.
                $this->incrementLoginAttempts($request);
                return $this->sendFailedLoginResponse($request);
            } else {
                return back()->with('error', '此邮箱未激活');
            }
        } else {
            return back()->with('error', '抱歉，此用户名未注册');
        }
    }


    /**
     * 多字段登录
     *
     * @param Request $request
     *
     * @return bool
     */
        public function attemptLogin(Request $request)
        {
            $name = $request->input('username');
            $password = $request->input('password');
            // 验证用户名登录方式
            $usernameLogin = $this->guard()->attempt(
                ['name' => $name, 'password' => $password], $request->has('remember')
            );
            if ($usernameLogin) {
                return true;
            }

            // 验证手机号登录方式
            $mobileLogin = $this->guard()->attempt(
                ['phone' => $name, 'password' => $password], $request->has('remember')
            );
            if ($mobileLogin) {
                return true;
            }

            // 验证邮箱登录方式
            $emailLogin = $this->guard()->attempt(
                ['email' => $name, 'password' => $password], $request->has('remember')
            );
            if ($emailLogin) {
                return true;
            }

            return false;
        }

    /**默认用户名登录
     * @return string
     */
        public function username()
        {
            return 'username';
        }

    /** 加入验证码验证
     *
     * @param Request $request
     */
        protected function validateLogin( Request $request )
        {
            $this->validate($request, [ 'captcha' => 'captcha|required', $this->username() => 'required|string', 'password' => 'required|string', ]);
        }

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/admin/index';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}
